// pages/me/me.js
const Request = require("../../utils/util");//导入模块
const QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');

var map = new QQMapWX({
  key: 'DSOBZ-Y4IWR-U4GWZ-WIAPJ-WWG5Q-X4BTP' // 必填 
});

Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatar: "",
    name: "",
    oneButton: [{ text: '确定' }],
    clockInRecordDialogShow: false,
    clockRecords: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userInfo = JSON.parse(wx.getStorageSync('userInfo'));
    this.setData({
      avatar: userInfo.userName,
      name: userInfo.userName
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 打卡
   */
  clockIn: function () {

    let _this = this;
    wx.getSetting({
      success: (res) => {
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
          wx.showModal({
            title: '请求授权当前位置',
            content: '需要获取您的地理位置，请确认授权',
            success: function (res) {
              if (res.cancel) {
                wx.showToast({
                  title: '拒绝授权',
                  icon: 'none',
                  duration: 1000
                })
              } else if (res.confirm) {
                wx.openSetting({
                  success: function (dataAu) {
                    if (dataAu.authSetting["scope.userLocation"] == true) {
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 1000
                      })
                      //再次授权，调用wx.getLocation的API
                      _this.getLocationAddress();
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'none',
                        duration: 1000
                      })
                    }
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == undefined) {
          //调用wx.getLocation的API
          this.getLocationAddress();
        }
        else {
          //调用wx.getLocation的API
          this.getLocationAddress();
        }
      }
    })


  },

  /**
   * 有权限就获取地理位置
   */
  getLocationAddress: function () {
    let _this = this;
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        map.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: function (val) {
            _this.uploadingAddress(res.latitude, res.longitude, val.result.address);
          },
          fail: function (err) {
            console.log(err);
            wx.showToast({
              title: '获取定位信息失败!',
              icon: 'none',
              duration: 1000
            })
          },
        })
      },
      fail(err) {
        wx.showToast({
          title: '获取定位信息失败!',
          icon: 'none',
          duration: 1000
        })
      }
    })
  },

  /**
   * 上传经纬度地址
   * @param {*} x 纬度
   * @param {*} y 经度
   */
  uploadingAddress: function (x, y, a) {
    wx.showLoading({
      title: '打卡中',
      mask: true,
    })

    let parameter = {
      longitude: y,
      latitude: x,
      address: a,
    }
    Request.post("emClock/insertEmClock", parameter).then(res => {
      wx.hideLoading();
      wx.showToast({
        title: '打卡成功',
        icon: 'none',
        duration: 1000
      })

    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err.data.return_msg,
        icon: 'none',
        duration: 1000
      })
    })
  },


  /**
   * 录入入口
   */
  enter: function () {
    wx.navigateTo({
      url: '/pages/enter/enter',
    })
  },



  /**
   * 查看打卡记录
   */
  queryChinkInRecords: function () {
    wx.showLoading({
      title: '获取打卡记录',
      mask: true,
    })

    Request.get("emClock/getEmClockList", {}).then(res => {
      wx.hideLoading();
      this.setData({ clockRecords: res })
      this.openCIRDialog();
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err.data.return_msg,
        icon: 'none',
        duration: 1000
      })
    })
  },
  /**
   * 打开dialog
   */
  openCIRDialog: function () {
    this.setData({
      clockInRecordDialogShow: true
    })
  },
  /**
   * 关闭dialog
   */
  tapCIRButton: function () {
    this.setData({
      clockInRecordDialogShow: false
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})